<?php
namespace app\index\controller;

class MysqlPdo
{
    //定义属性
    protected $link1;
//    protected $link1;
    protected $redis;

    /**
     * 连接数据库
     * Mysql constructor.
     */
    public function __construct()
    {
        $this->link1 = new \PDO('mysql:host=127.0.0.1;dbname=1126', 'root', 'root');
//        $this->redis = Redis();
//        $this->redis->connect('127.0.0.1','6379');
    }

    /**
     * 添加数据
     * @param $tableName
     * @param $arr
     * @return false|string
     */
    public function getAdd($tableName,$data)
    {
        $type = $data['type'];
        $book = $data['book'];
        $title = $data['title'];
        $number = $data['number'];
        $name = $data['name'];
        $time = $data['time'];
        $sql = "insert into $tableName values (null,'$type','$book','$title','$number','$name','$time')";
        $res = $this->link1->exec($sql);
        return $res;
    }

    /**
     * 展示数据和分页搜素
     * @param $tableName
     * @param $arr
     * @return false|string
     */
    public function getShow($tableName,$arr)
    {

        $sql1 = "select * from $tableName  ";
        $total = $this->link1->query($sql1)->rowCount();
        $sql = "select * from $tableName  ";
        $limit = " limit 6";
        if(!empty($arr)){
//            $query_redis = $this->redis->get($arr['book']);
//            if($query_redis){
//                echo "redis";
//                return json_encode($query_redis,true);
//            }
            $where = ' where ';
            foreach($arr as $key=>$value){
                $where .= "$key".'='."'$value'";
            }
            $sql .= $where.$limit;
        }else{
            $sql .=$limit;
        }
        $stmt = $this->link1->query($sql);
        $res = $stmt->fetchAll(2);
//        if(!empty($arr)){
//            $this->redis->set($arr['book'],json_encode($res));
//        }
        if($res){
            $arr1['code'] = 0;
            $arr1['msg'] = 'ok';
            $arr1['total'] = $total;
            $arr1['data'] = $res;
        }else{
            $arr1['code'] = 1;
            $arr1['msg'] = 'no';
            $arr1['data'] = [];
        }
        return json_encode($arr1);
    }
    /**
     * 删除数据
     * @param $tableName
     * @param $arr
     * @return false|string
     */
    public function getDel($tableName,$arr)
    {
        $sql = "delete from $tableName  ";
        if(!empty($arr)){
            $where = ' where ';
            foreach($arr as $key=>$value){
                $where .= "$key".'='."'$value'";
            }
            $sql .= $where;
        }
        $res = $this->link1->exec($sql);
        if($res){
            $arr1['code'] = 0;
            $arr1['msg'] = '删除成功';
            $arr1['data'] = [];
        }else{
            $arr1['code'] = 1;
            $arr1['msg'] = '删除失败';
            $arr1['data'] = [];
        }
        return json_encode($arr1);
    }

    /**
     * 修改数据
     * @param $tableName
     * @param $data
     * @param $where
     * @return false|string
     */
    public function getUpdate($tableName,$data,$where)
    {
        $sql = "update $tableName set ";
        if(!empty($data)){
            $data1 = '';
            $where1 = ' where ';
            foreach($data as $key=>$value){
                $data1 .= "$key".'='."'$value'";
            }
            foreach($where as $key=>$value){
                $where1 .= "$key".'='."'$value'";
            }
            $sql .= $data1.$where1;
        }

        $res = $this->link1->exec($sql);
        if($res){
            $arr1['code'] = 0;
            $arr1['msg'] = '修改成功';
            $arr1['data'] = [];
        }else{
            $arr1['code'] = 1;
            $arr1['msg'] = '修改失败';
            $arr1['data'] = [];
        }
        return json_encode($arr1);
    }


}